class Solution {
public:
    int maximumLength(string s)
    {
        int n = s.size();
        int ans = -1;
        int right = 0;
        int left = 0;
        unordered_map<char, int> hash;
        int flag = 0;
        while (right < n)
        {
            int sum = 0;
            hash[s[right]]++;
            size_t test = left + 1;
            int cnt = 1;
            if (hash.size() == 1)
            {
                while (1)
                {
                    test = s.find(s.substr(left, right - left + 1), test);
                    if (test == string::npos) break;
                    cnt++;
                    if (cnt == 3)
                    {
                        sum = right - left + 1;
                        break;
                    }
                    test = test + 1;
                }
            }
            else
            {
                while (hash.size() > 1)
                {
                    if (--hash[s[left]] == 0) hash.erase(s[left]);
                    left++;
                }
            }
            if (cnt == 3 && hash.size() == 1) ans = ans > sum ? ans : sum;
            right++;
        }
        if (ans == -1)
        {
            for (int i = 0; i < n; i++)//最后检查一遍每个有没有漏的
            {
                int cnt = 0;
                size_t test = 0;

                while (1)
                {
                    test = s.find(s[i], test);
                    if (test == string::npos) break;
                    cnt++;
                    if (cnt == 3) break;
                    test = test + 1;
                }
                if (cnt == 3) ans = 1;
            }
        }
        return ans;
    }
};